package com.wsc.wscbirthdaykeeper.adapter;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wscbirthdaykeeper.R;
import com.wsc.wscbirthdaykeeper.managers.ResourceManager;
import com.wsc.wscbirthdaykeeper.model.entity.Person;
import com.wsc.wscbirthdaykeeper.utils.DateUtils;

@SuppressLint("SimpleDateFormat")
public class PersonAdapter extends BaseAdapter {
	private SimpleDateFormat sdf;
	private LayoutInflater mInflater;
	private List<Person> person = Collections.emptyList();

	private final Context context;

	public PersonAdapter(Context context) {
		this.context = context;
		sdf = new SimpleDateFormat(ResourceManager.getStringValue(R.string.format_date));
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void updatePersons(List<Person> persons) {
		this.person = persons;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return person.size();
	}

	@Override
	public Person getItem(int position) {
		return person.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView image;
		TextView name;
		TextView date;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_person, null);
			image = (ImageView) convertView.findViewById(R.id.person_image);
			name = (TextView) convertView.findViewById(R.id.person_name);
			date = (TextView) convertView.findViewById(R.id.person_date);
			convertView.setTag(new ViewHolder(image, name, date));
		} else {
			ViewHolder viewHolder = (ViewHolder) convertView.getTag();
			image = viewHolder.image;
			name = viewHolder.name;
			date = viewHolder.date;
		}

		Person person = getItem(position);

		name.setText(person.getName());

		int days = DateUtils.getWaitDays(person.getDate());

		switch (days) {
		case 0:
			date.setText(ResourceManager.getStringValue(R.string.person_today) + " (" + sdf.format(person.getDate()) + ")");
			date.setTextColor(ResourceManager.getColorValue(R.color.red));
			date.setTextSize(ResourceManager.getDimen(R.dimen.dimen_13));
			date.setTypeface(null, Typeface.BOLD);
			break;
		case 1:
			date.setText(ResourceManager.getStringValue(R.string.person_tomorrow) + " (" + sdf.format(person.getDate()) + ")");
			date.setTextColor(ResourceManager.getColorValue(R.color.green));
			date.setTextSize(ResourceManager.getDimen(R.dimen.dimen_13));
			date.setTypeface(null, Typeface.BOLD);
			break;
		default:
			date.setText(ResourceManager.getStringValue(R.string.person_through) + " " + days + " " + ResourceManager.getStringValue(R.string.person_days) + " (" + sdf.format(person.getDate()) + ")");
			date.setTextColor(Color.WHITE);
			date.setTextSize(ResourceManager.getDimen(R.dimen.dimen_11));
			date.setTypeface(null, Typeface.NORMAL);
		}

		// Animation animation = AnimationUtils.loadAnimation(context,
		// R.anim.anim_list_view);
		// convertView.startAnimation(animation);

		return convertView;
	}

	private static class ViewHolder {
		public final ImageView image;
		public final TextView name;
		public final TextView date;

		public ViewHolder(ImageView image, TextView name, TextView date) {
			this.image = image;
			this.name = name;
			this.date = date;
		}
	}

}
